package cn.zero.letaomallspringboot.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.zero.letaomallspringboot.entity.Admin;
import cn.zero.letaomallspringboot.exception.CustomException;
import cn.zero.letaomallspringboot.mapper.AdminMapper;
import cn.zero.letaomallspringboot.service.IAdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AdminService implements IAdminService {
    private final AdminMapper adminMapper;

    public AdminService(AdminMapper userMapper) {
        this.adminMapper = userMapper;
    }

    public Admin login(String username, String password, boolean rememberMe) {
        if (username.isEmpty() || password.isEmpty()) {
            throw new CustomException("用户名或密码不能为空");
        }
        Admin user = adminMapper.selectByName(username);
        if (user == null) {
            throw new CustomException("用户不存在, 请重新尝试");
        }
        if (BCrypt.checkpw(password, user.getPassword())) {
            user.setPassword(null);
            user.setUsername(username);
            return user;
        } else {
            throw new CustomException("用户密码错误，请重新尝试");
        }
    }

    public List<Admin> user() {
        List<Admin> list = adminMapper.selectAll();
        list.forEach(admin -> admin.setPassword(null));
        return list;
    }

    @Override
    public Admin getById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if (admin != null) {
            admin.setPassword(null);  // 关键：永远不返回密码哈希
        }
        return admin;
    }

    @Override
    @Transactional
    public boolean addAdmin(Admin admin) {
        // 校验重复
        Admin exist = adminMapper.selectByUsernameOrEmail(admin.getUsername(), admin.getEmail());
        if (exist != null) {
            return false;
        }

        // 关键：对明文密码进行 BCrypt 加密
        if (StrUtil.isNotBlank(admin.getPassword())) {
            admin.setPassword(BCrypt.hashpw(admin.getPassword(), BCrypt.gensalt()));
        } else {
            throw new CustomException("密码不能为空");
        }

        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setEnable((byte) 1);
        adminMapper.insert(admin);
        return true;
    }

    // 修改管理员 → 如果传了密码才更新，且加密
    @Override
    @Transactional
    public boolean updateAdmin(Admin admin) {
        Admin exist = adminMapper.selectById(admin.getUid());
        if (exist == null) {
            return false;
        }

        // 邮箱冲突检查
        Admin emailExist = adminMapper.selectByEmailExceptId(admin.getEmail(), admin.getUid());
        if (emailExist != null) {
            return false;
        }

        admin.setUpdateTime(new Date());
        adminMapper.update(admin);
        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if (admin == null) {
            return false;
        }
        adminMapper.deleteById(id);
        return true;
    }

    @Override
    public List<Admin> search(String username, String email, String role) {
        return adminMapper.search(username, email, role);
    }

    // Service 新增方法
    public boolean changePassword(Integer uid, String newPassword) {
        String hashed = BCrypt.hashpw(newPassword, BCrypt.gensalt());
        int rows = adminMapper.updatePasswordById(uid, hashed, new Date());
        return rows > 0;
    }
}
